/**
 * 
 */
package com.sysmanage.etl.service.impl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sysmanage.etl.dao.CovertFactoryDao;
import com.sysmanage.etl.dao.PropertyColumnDao;
import com.sysmanage.etl.dao.PropertyDao;
import com.sysmanage.etl.dao.TableDao;
import com.sysmanage.etl.model.ETL_DataSource;
import com.sysmanage.etl.model.ETL_Property;
import com.sysmanage.etl.model.ETL_Property_Column;
import com.sysmanage.etl.model.ETL_Table;
import com.sysmanage.etl.service.ITableService;
import com.sysmanage.etl.utils.PropertyUtils;
import com.sysmanage.framework.dao.hspring.DAOInterface;
import com.sysmanage.framework.service.BaseService;

/**
 * @author Yu Jou
 * 2014年7月14日
 */
@Service("tableService")
@Transactional
public class TableServiceImpl extends BaseService<ETL_Table> implements ITableService {

	@Resource
	private TableDao tableDao;
	@Resource
	private PropertyDao propertyDao;
	@Resource
	private PropertyColumnDao pcDao;
	@Resource
	private CovertFactoryDao covertFactoryDao;

	@Override
	protected DAOInterface<ETL_Table> getDAO() {
		
		return tableDao;
	}

	@Override
	public boolean deleteTable(Serializable tableId) {
		ETL_Table table = this.tableDao.findById(tableId);
		
		Set<ETL_Property> propertyList = table.getPropertys();;
		for (ETL_Property property : propertyList) {
			
			this.propertyDao.delete(property);
		}
		return this.tableDao.delete(table);
	}
	
	@Override
	public boolean alterTable(ETL_Table table) {
		table.setDisplay(true);
		tableDao.alter(table);
		
		if (table.getPropertys() == null || table.getPropertys().size() <= 0) {
			ETL_DataSource ds = table.getDataSource();
			List<ETL_Property> properties =  PropertyUtils.change2ETL_Property(ds, table.getTable_name());
			for (ETL_Property proerty : properties) {
				proerty.setTable(table);
				propertyDao.save(proerty);
			}
		}
		return true;
	}
	
	@Override
	public boolean deleteTable(ETL_Table table) {
		table.setDisplay(false);
		tableDao.alter(table);
		
		if (table.getPropertys() != null && table.getPropertys().size() > 0) {
			for (ETL_Property property : table.getPropertys()) {
				covertFactoryDao.executeSql("delete from ETL_CovertFactory where property_id = " + property.getProperty_id());
				propertyDao.delete(property);
			}
		}
		return true;
	}
	
	
	@Override
	public void reloadProperty(ETL_Table table, List<ETL_Property> properties) {
		if (properties == null || properties.size() <= 0) {
			return;
		}
		List<ETL_Property> havaProperties = propertyDao.findList("from ETL_Property p where p.table.table_id = " + table.getTable_id());
		
		if (havaProperties == null || havaProperties.size() <= 0) {
			for ( ETL_Property property : properties ) {
				property.setTable(table);
				propertyDao.save(property);
			}
		} else {
			Map<String, ETL_Property> currentMap = new HashMap<String, ETL_Property>();
			Map<String, ETL_Property> haveMap = new HashMap<String, ETL_Property>();
			for (ETL_Property property : properties) {
				currentMap.put(property.getProperty_name(), property);
			}
			
			for (ETL_Property property : havaProperties) {
				haveMap.put(property.getProperty_name(), property);
			}
			
			for (String propertyName : currentMap.keySet()) {
				ETL_Property property = haveMap.get(propertyName);
				if (property == null) {
					property = currentMap.get(propertyName);
					property.setTable(table);
					propertyDao.save(property);
				}
			}
			
			for (String propertyName : haveMap.keySet()) {
				ETL_Property property = currentMap.get(propertyName);
				if (property == null) {
					property = haveMap.get(propertyName);
					List<ETL_Property_Column> pcs = pcDao.findList("from ETL_Property_Column pc where pc.property.property_id = " + property.getProperty_id());
					if (pcs == null || pcs.size() <= 0) {
						covertFactoryDao.executeSql("delete from ETL_CovertFactory where property_id = " + property.getProperty_id());
						propertyDao.delete(property);
					} else {
						property.setProperty_exists(false);
						propertyDao.alter(property);
					}
				}
			}
		}
	}
	

}
